﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace Public
{ /*条码打印命令说明
    ^XA //条码打印指令开始
    ^MD30 //设置色带颜色的深度, 取值范围从-30到30
    ^LH60,10 //设置条码纸的边距
    ^FO20,10 //设置条码左上角的位置
    ^ACN,18,10 //设置字体
    ^BY1.4,3,50 //设置条码样式。1.4是条码的缩放级别，3是条码中粗细柱的比例, 50是条码高度
    ^BC,,Y,N //打印code128的指令
    ^FD12345678^FS //设置要打印的内容, ^FD是要打印的条码内容^FS表示换行
    ^XZ //条码打印指令结束
    */
    //上面的指令会打印12345678的CODE128的条码
    public class BarcodePrint
    {
        private const uint GENERIC_READ = 0x80000000;
        private const uint GENERIC_WRITE = 0x40000000;
        private const int OPEN_EXISTING = 3;


        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool CloseHandle(int hObject);
        private static int iHandle;
        public bool Open(string portname)
        {
            //iHandle = CreateFile("LPT1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
            iHandle = CreateFile(portname + ":", 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
            //iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);
            if (iHandle != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void OO()
        {

        }
        public bool Write(string Mystring, string portname)
        {
            if (iHandle != -1)
            {
                int i;
                OVERLAPPED x;
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
            }
            else
            {
                throw new Exception(portname + "端口未打开!");
            }
        }
        public bool Close()
        {
            return CloseHandle(iHandle);
        }



        /// <summary>  
        /// 获取本地已安装的打印机  
        /// </summary>  
        /// <returns></returns>  
        public List<string> GetPrinter()
        {
            List<string> Printer_List = new List<string>();
            System.Drawing.Printing.PrinterSettings.StringCollection PrinterList = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
            string ss = GetDefault();
            if (!string.IsNullOrEmpty(ss))
            {
                Printer_List.Add(ss); //默认打印机始终出现在列表的第一项  
            }
            foreach (var item in PrinterList)
            {
                if (!Printer_List.Contains(item.ToString()))
                {
                    Printer_List.Add(item.ToString());
                }
            }
            return Printer_List;
        }


        /// <summary>  
        /// 获取默认打印机名称  
        /// </summary>  
        /// <returns></returns>  
        public string GetDefault()
        {
            System.Drawing.Printing.PrintDocument p = new System.Drawing.Printing.PrintDocument();
            return p.DefaultPageSettings.PrinterSettings.PrinterName;
        }
        /// <summary>
        /// 设置为默认打印机 
        /// </summary>
        /// <param name="Name">打印机名称</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机 
        /// <summary>  
        /// 获取可用的端口  
        /// </summary>  
        /// <returns></returns>   
        public List<string> GetComList()
        {
            List<string> ComList = new List<string>();
            Microsoft.VisualBasic.Devices.Ports pc = new Microsoft.VisualBasic.Devices.Ports();
            ComList.Add("LPT1");//LPT的端口获取了，直接写个
            foreach (string s in pc.SerialPortNames)
            {
                ComList.Add(s);
            }
            return ComList;
        }
    }

}
